package com.cskaoyan.service;

import com.cskaoyan.bean.market.Keyword;
import com.cskaoyan.bean.market.KeywordExample;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.market.KeywordCreateVo;
import com.cskaoyan.bean.market.KeywordListBean;
import com.cskaoyan.bean.market.KeywordListVo;
import com.cskaoyan.mapper.KeywordMapper;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class KeywordServiceImpl implements KeywordService {

    @Autowired
    KeywordMapper keywordMapper;

    @Override
    public KeywordListVo list(BaseParam baseParam,String keyword0,String url) {
        PageUtil.start(baseParam);
        KeywordExample keywordExample = new KeywordExample();
        keywordExample.setOrderByClause(baseParam.getSort() + " " + baseParam.getOrder());
        if(keyword0 != null && !"".equals(keyword0)){
            keywordExample.createCriteria().andKeywordLike("%" + keyword0 + "%");
        }
        if(url != null && !"".equals(url)){
            keywordExample.createCriteria().andUrlLike("%" + url + "%");
        }
        List<Keyword> keywords = keywordMapper.selectByExample(keywordExample);
        PageInfo<Keyword> keywordPageInfo = new PageInfo<>(keywords);
        long total = keywordPageInfo.getTotal();
        int pages = keywordPageInfo.getPages();
        int pageNum = keywordPageInfo.getPageNum();
        Integer limit = baseParam.getLimit();
        ArrayList<KeywordListBean> keywordListBeans = new ArrayList<>();
        for (Keyword keyword : keywords) {
            if(keyword.getDeleted() == false){
                KeywordListBean keywordListBean = new KeywordListBean();
                keywordListBean.setId(keyword.getId());
                keywordListBean.setAddTime(keyword.getAddTime());
                keywordListBean.setDefault(keyword.getIsDefault());
                keywordListBean.setDeleted(keyword.getDeleted());
                keywordListBean.setHot(keyword.getIsHot());
                keywordListBean.setKeyword(keyword.getKeyword());
                keywordListBean.setSortOrder(keyword.getSortOrder());
                keywordListBean.setUpdateTime(keyword.getUpdateTime());
                keywordListBean.setUrl(keyword.getUrl());
                keywordListBeans.add(keywordListBean);
            }
        }
        KeywordListVo keywordListVo = new KeywordListVo();
        keywordListVo.setLimit(limit);
        keywordListVo.setList(keywordListBeans);
        keywordListVo.setPage(pageNum);
        keywordListVo.setPages(pages);
        keywordListVo.setTotal((int) total);
        return keywordListVo;
    }

    @Override
    public KeywordCreateVo create(Map map) {
        Keyword keyword = new Keyword();
        String keyword1 = (String) map.get("keyword");
        KeywordExample keywordExample1 = new KeywordExample();
        keywordExample1.createCriteria().andKeywordEqualTo(keyword1).andDeletedEqualTo(false);
        List<Keyword> keywords1 = keywordMapper.selectByExample(keywordExample1);
        if(keywords1.size() == 1){
            //不能创建重复的keyword
            return null;
        }
        keyword.setKeyword(keyword1);
        Boolean isDefault = (Boolean) map.get("isDefault");
        keyword.setIsDefault(isDefault);
        Boolean isHot = (Boolean) map.get("isHot");
        keyword.setIsHot(isHot);
        Date time = new Date();
        keyword.setAddTime(time);
        keyword.setUpdateTime(time);
        keywordMapper.insertSelective(keyword);
        Integer id = keyword.getId();
        KeywordCreateVo keywordCreateVo = new KeywordCreateVo();
        keywordCreateVo.setId(id);
        keywordCreateVo.setKeyword(keyword1);
        keywordCreateVo.setDefault(isDefault);
        keywordCreateVo.setHot(isHot);
        keywordCreateVo.setAddTime(time);
        keywordCreateVo.setUpdateTime(time);
        return keywordCreateVo;
    }

    @Override
    public void delete(KeywordListBean keywordDeleteBo) {
        Keyword keyword = new Keyword();
        keyword.setDeleted(true);
        Date updateTime = new Date();
        keyword.setUpdateTime(updateTime);
        keyword.setId(keywordDeleteBo.getId());
        keywordMapper.updateByPrimaryKeySelective(keyword);
    }

    @Override
    public Keyword update(Keyword keyword) {
//        KeywordExample keywordExample = new KeywordExample();
//        keywordExample.createCriteria().andIdEqualTo(keyword.getId());
        Date updateTime = new Date();
        keyword.setUpdateTime(updateTime);
        keywordMapper.updateByPrimaryKeySelective(keyword);
        return keyword;
    }
}
